package com.my.blog.website.controller.admin;

import com.github.pagehelper.PageInfo;
import com.my.blog.website.constant.WebConst;
import com.my.blog.website.controller.BaseController;
import com.my.blog.website.dto.LogActions;
import com.my.blog.website.exception.TipException;
import com.my.blog.website.model.Bo.BackResponseBo;
import com.my.blog.website.model.Bo.RestResponseBo;
import com.my.blog.website.model.Vo.ContentVo;
import com.my.blog.website.model.Vo.HolidayVo;
import com.my.blog.website.model.Vo.OptionVo;
import com.my.blog.website.service.IHolidayService;
import com.my.blog.website.service.ILogService;
import com.my.blog.website.service.IOptionService;
import com.my.blog.website.service.ISiteService;
import com.my.blog.website.utils.GsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/holiday")
public class HolidayController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HolidayController.class);
    @Resource
    private IHolidayService holidayService;

    @Resource
    private ILogService logService;

    @Resource
    private ISiteService siteService;

    /**
     * 系统设置
     */
    @GetMapping(value = "")
    public String setting(HttpServletRequest request) {
//        List<OptionVo> voList = optionService.getOptions();
//        Map<String, String> options = new HashMap<>();
//        voList.forEach((option) -> {
//            options.put(option.getName(), option.getValue());
//        });
//        if (options.get("site_record") == null) {
//            options.put("site_record", "");
//        }
//        request.setAttribute("options", options);
        return "admin/holiday";
    }
    @RequestMapping(value="holiday_list")
    public String setting1(HttpServletRequest request) {
        HolidayVo holidayVo=new HolidayVo();
        List<HolidayVo> holidayVoPageInfo = holidayService.searchHolidayVo();
        request.setAttribute("holiday", holidayVoPageInfo);
        return "admin/holiday_list";
    }


    @GetMapping(value = "/{id}")
    public String Holiday_edit(@RequestParam("id") String id,HttpServletRequest request) {
        //ContentVo contents = contentsService.getContents(cid);
        //request.setAttribute("contents", contents);
        HolidayVo h=new HolidayVo();
        System.out.println("id:"+id+"---------------------");
        h=holidayService.searchHolidayVoById(id);
        request.setAttribute("holidayContentByid", h);
        return "admin/holiday_edit";
    }
    @RequestMapping(value = "fy")
    @ResponseBody
    public RestResponseBo Holiday_modify(@RequestParam("id") String id1,@RequestParam String name, @RequestParam String start_time,@RequestParam String over_time,@RequestParam String money,HttpServletRequest request) {
        //ContentVo contents = contentsService.getContents(cid);
        //request.setAttribute("contents", contents);
        System.out.println(id1+"-----------------------------");
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = null;
        Date d2= null;
        long diff=0L;
        try {
            d1 = df.parse(start_time);
            d2=df.parse(over_time);
             diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别
        } catch (ParseException e) {
            return RestResponseBo.fail("初始时间与结束时间输入有误");
        }



        int id=Integer.parseInt(id1);


        HolidayVo h=new HolidayVo();
        h.setId(id);h.setName(name);h.setStart_time(start_time);h.setOver_time(over_time);
        h.setMoney(Double.parseDouble(money));
        int i=0;
              i=  holidayService.updateHolidayByid(h);
             System.out.println(diff+"---------------------------------");
      if(i!=0&&diff>0.000) {
          return RestResponseBo.ok();
      }
      else{
          return RestResponseBo.fail("初始时间与结束时间输入有误");
      }
    }

    @RequestMapping(value = "modify1")
    @ResponseBody
    public RestResponseBo Holiday_modify1(@RequestParam("id") String id1,@RequestParam String name, @RequestParam String start_time,@RequestParam String over_time,@RequestParam String money,HttpServletRequest request) {
  System.out.println(id1+"=------------------------");
        System.out.println(start_time+"=------------------------");

        System.out.println(over_time+"=------------------------");

        System.out.println(name+"=------------------------");

        return RestResponseBo.ok();
    }
        /**
     * 保存系统设置
     */
    @PostMapping(value = "")
    @ResponseBody
    public RestResponseBo saveSetting(@RequestParam String Holiday_name, @RequestParam String start_time,@RequestParam String over_time,@RequestParam String money,HttpServletRequest request) {
        try {
//            Map<String, String[]> parameterMap = request.getParameterMap();
//            HolidayVo  h=new HolidayVo();
//            System.out.println("主题:"+site_theme);
//            Map<Object, Object> querys = new HashMap<>();
//            parameterMap.forEach((key, value) -> {
//                querys.put(key, join(value));
//            });
            System.out.println("---------------------------------------------");
            HolidayVo  h=new HolidayVo();
            h.setMoney(Double.parseDouble(money));
            System.out.println("钱数"+money);
            h.setStart_time(start_time);
            System.out.println("开始时间:"+start_time);
            h.setName(Holiday_name);
            System.out.println(Holiday_name+"---------------------------");
            h.setOver_time(over_time);
            System.out.println("结束时间:"+over_time);
            holidayService.saveHoliday(h);
            System.out.println("---------------------------------------------");

           // WebConst.initConfig = querys;
//            if (StringUtils.isNotBlank(site_theme)) {
//                BaseController.THEME = "themes/" + site_theme;
//            }
           // logService.insertLog(LogActions.SYS_SETTING.getAction(), GsonUtils.toJsonString(querys), request.getRemoteAddr(), this.getUid(request));
            return RestResponseBo.ok();
        } catch (Exception e) {
            String msg = "保存设置失败";
            return RestResponseBo.fail(msg);
        }
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public RestResponseBo delete(@RequestParam int id, HttpServletRequest request) {
      //String result = contentsService.deleteByCid(cid);
       // logService.insertLog(LogActions.DEL_ARTICLE.getAction(), cid + "", request.getRemoteAddr(), this.getUid(request));
        String result=holidayService.deleteByCid(id);
        if (!WebConst.SUCCESS_RESULT.equals(result)) {
            return RestResponseBo.fail(result);
        }
        return RestResponseBo.ok();
    }
    /**
     * 系统备份
     *
     * @return
     */
    @PostMapping(value = "backup")
    @ResponseBody
    public RestResponseBo backup(@RequestParam String bk_type, @RequestParam String bk_path,
                                 HttpServletRequest request) {
        if (StringUtils.isBlank(bk_type)) {
            return RestResponseBo.fail("请确认信息输入完整");
        }
        try {
            BackResponseBo backResponse = siteService.backup(bk_type, bk_path, "yyyyMMddHHmm");
            //logService.insertLog(LogActions.SYS_BACKUP.getAction(), null, request.getRemoteAddr(), this.getUid(request));
            return RestResponseBo.ok(backResponse);
        } catch (Exception e) {
            String msg = "备份失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
    }


    /**
     * 数组转字符串
     *
     * @param arr
     * @return
     */
    private String join(String[] arr) {
        StringBuilder ret = new StringBuilder();
        String[] var3 = arr;
        int var4 = arr.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            String item = var3[var5];
            ret.append(',').append(item);
        }

        return ret.length() > 0 ? ret.substring(1) : ret.toString();
    }

}
